package prototype;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import javax.swing.*;
import java.awt.*;

public class WeeklyUsageGraph {

    private final JFreeChart chart;
    private JButton schedule;
    private JButton dailyUsage;

    public WeeklyUsageGraph() {

        final XYSeries powerplant = new XYSeries("Powerplant");
        powerplant.add(0.0, 50.0);
        powerplant.add(168.0, 50.0);

        final XYSeries house = new XYSeries("House");
        house.add(0.0, 30.0);
        house.add(10.0, 50.0);
        house.add(20.0, 60.0);
        house.add(30.0, 65.0);
        house.add(40.0, 20.0);
        house.add(50.0, 15.0);
        house.add(60.0, 35.0);
        house.add(70.0, 30.0);
        house.add(80.0, 45.0);
        house.add(90.0, 50.0);
        house.add(100.0, 30.0);
        house.add(110.0, 10.0);
        house.add(120.0, 5.0);
        house.add(130.0, 35.0);
        house.add(140.0, 40.0);
        house.add(150.0, 45.0);
        house.add(160.0, 50.0);
        house.add(168.0, 40.0);

        XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(powerplant);
        dataset.addSeries(house);

        chart = ChartFactory.createXYLineChart("Weekly Usage", "Hours",
                "Usage", dataset, PlotOrientation.VERTICAL, true, true, false);
        schedule = new JButton("Schedule");
        dailyUsage = new JButton("Daily Usage");
    }

    public ChartPanel createGraph() {
        final ChartPanel chartPanel = new ChartPanel(chart);
        GridBagLayout gbl_chartPanel = new GridBagLayout();
        gbl_chartPanel.columnWidths = new int[]{633, 0, 101, 0};
        gbl_chartPanel.rowHeights = new int[]{538, 0, 0, 23, 0};
        gbl_chartPanel.columnWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
        gbl_chartPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
        chartPanel.setLayout(gbl_chartPanel);

        GridBagConstraints gbc_schedule = new GridBagConstraints();
        gbc_schedule.insets = new Insets(0, 0, 5, 5);
        gbc_schedule.anchor = GridBagConstraints.EAST;
        gbc_schedule.gridx = 1;
        gbc_schedule.gridy = 2;
        chartPanel.add(schedule, gbc_schedule);

        GridBagConstraints gbc_dailyUsage = new GridBagConstraints();
        gbc_dailyUsage.insets = new Insets(0, 0, 5, 5);
        gbc_dailyUsage.anchor = GridBagConstraints.EAST;
        gbc_dailyUsage.gridx = 0;
        gbc_dailyUsage.gridy = 2;
        chartPanel.add(dailyUsage, gbc_dailyUsage);

        return chartPanel;
    }

    public JButton getScheduleButton() {
        return schedule;
    }

    public JButton getDailyUsage() {
        return dailyUsage;
    }
}
